package com.metering.purchaseServerAdmin.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.metering.purchaseServerAdmin.domain.RoleDO;
import com.metering.purchaseServerAdmin.service.RoleService;
import com.metering.purchaseServerCommon.common.bean.util.PageUtils;
import com.metering.purchaseServerCommon.common.bean.util.Query;
import com.metering.purchaseServerCommon.common.bean.util.R;

/**
 * 角色
 * @ClassName: RoleController
 * @Description: 
 * @Author GA
 * @DateTime 2018年12月17日 上午10:02:43
 */
@RequestMapping("/role")
@RestController
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;
	
	@GetMapping()
    PageUtils list(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<RoleDO> roleDOS = roleService.list(query);
        int total = roleService.count(query);
        PageUtils pageUtil = new PageUtils(roleDOS, total);
        return pageUtil;
    }

    @GetMapping("/userId/{userId}")
    List<Long> roleIdByUserId(@PathVariable Long userId){
        return roleService.RoleIdsByUserId(userId);
    }

    @PostMapping
    R save(@RequestBody RoleDO roleDO){
        if(roleService.save(roleDO)>0){
            return R.ok();
        }
        return R.error();
    }

    @PutMapping
    R update(@RequestBody RoleDO roleDO){
        if(roleService.update(roleDO)>0){
            return R.ok();
        }
        return R.error();
    }
}
